package com.cdbkj.cloud.pos;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.androidyang.library.common.utils.ToastUtils;
import com.androidyang.library.http.okhttp.bean.HttpResult;
import com.androidyang.library.http.okhttp.listener.HttpClientHandler;
import com.cdbkj.cloud.pos.bean.Filed;
import com.cdbkj.cloud.pos.bean.JsonResult;
import com.cdbkj.cloud.pos.http.HttpRequest;
import com.cdbkj.cloud.pos.utils.Tools;
import com.cdbkj.cloud.pos.utils.DateUtils;
import com.orhanobut.logger.Logger;

import java.util.Calendar;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, HttpClientHandler {

    protected Button mCfjfBtn;
    protected Button mMzghBtn;

    private TextView time_tv;
    private TextView weekDay_tv;
    private TextView dateTime_tv;

    private Runnable dateTimeRunnable = null;
    private Calendar calendar = null;
    private String userId = Tools.getAndroidOsSystemProperties();//产品sn作为序列号
    private String httpType = "";

    //配对蓝牙名称
    private String bluetoothName;
    //配对款台号
    private String counterNo;

    private String hisTime;

    final MainActivity activity = this;
    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Tools.dismissDialog();
            Tools.backgroundAlpha(activity,1);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);
        initView();
        //String sn = Tools.getAndroidOsSystemProperties();
        //Logger.d("-------序列号--------"+sn);
        httpType = "hisTime";
        HttpRequest.hisTime("hisTime",this, userId);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.cfjf_btn) {
            Intent intent = new Intent(this, PrescriptionActivity.class);
            intent.putExtra("hisTime",hisTime);
            intent.putExtra("bluetoothName",bluetoothName);
            intent.putExtra("counterNo",counterNo);
            startActivity(intent);
            startActivity(intent);
        } else if (view.getId() == R.id.mzgh_btn) {
            Intent intent = new Intent(this, RegActivity.class);
            intent.putExtra("hisTime",hisTime);
            intent.putExtra("bluetoothName",bluetoothName);
            intent.putExtra("counterNo",counterNo);
            startActivity(intent);
        }
    }

    private void initView() {
        mCfjfBtn = (Button) findViewById(R.id.cfjf_btn);
        mCfjfBtn.setOnClickListener(MainActivity.this);
        mMzghBtn = (Button) findViewById(R.id.mzgh_btn);
        mMzghBtn.setOnClickListener(MainActivity.this);

        time_tv = findViewById(R.id.time_tv);
        weekDay_tv = findViewById(R.id.weekDay_tv);
        dateTime_tv = findViewById(R.id.dateTime_tv);

        setDateTime();
    }

    public void setDateTime(){
        dateTimeRunnable = new Runnable() {
            @Override
            public void run() {
                calendar = Calendar.getInstance();
                time_tv.setText(DateUtils.getHour(calendar)+":"+DateUtils.getMinute(calendar));
                weekDay_tv.setText(DateUtils.getWeek(calendar));
                dateTime_tv.setText(DateUtils.getYear(calendar)+"年"+DateUtils.getMonth(calendar)+"月"+DateUtils.getDay(calendar)+"日");
                //每1秒执行一次
                mCfjfBtn.postDelayed(this, 60*1000);
            }
        };
        mCfjfBtn.postDelayed(dateTimeRunnable, 0);
    }

    @Override
    public void httpClientSuccess(Object o, HttpResult httpResult) {
        JsonResult json = (JsonResult) httpResult.getResult();
        if (json == null) {
            //ToastUtils.showShort(this,"服务器处理异常");
            Tools.showDialog("服务器处理异常，请联系管理员",activity,clickListener,clickListener);
            return;
        }
        if (!json.isSuccess()) {
            ToastUtils.showShort(this,json.getMsg());
            return;
        }
        if("hisTime".equals(httpType) && json.getData()==null){
            //ToastUtils.showShort(this,"his不能正常访问");
            Tools.showDialog("不能正常访问 his接口，请联系管理员",activity,clickListener,clickListener);
            return;
        }
        if("hisTime".equals(httpType) && json.isSuccess()){
         //   Map resultMap = (Map)json.getData();
            hisTime=json.getData().toString();
            httpType = "deviceInfo";
            HttpRequest.getDeviceInfo("deviceInfo",this, userId);
            return;
        }
        if("deviceInfo".equals(httpType)){
            if(json.getData()==null){
                Tools.showDialogWithReturnMainActivity("请先为设备【"+userId+"】在后台配置相关信息！",this);
                return;
            }
            Map map = (Map)json.getData();
            if(map.get("bluetoothName")==null){
                Tools.showDialogWithReturnMainActivity("设备【"+userId+"】蓝牙信息未在后台配置！",this);
                return;
            }
            if(map.get("pairNo")==null){
                Tools.showDialogWithReturnMainActivity("设备【"+userId+"】款台号信息未在后台配置！",this);
                return;
            }
            bluetoothName = map.get("bluetoothName").toString();
            counterNo = map.get("pairNo").toString();
        }
    }

    @Override
    public void httpClientError(Object o, HttpResult httpResult) {
        //ToastUtils.showShort(this,"网络异常");
        Tools.showDialog("网络连接异常，请联系管理员",activity,clickListener,clickListener);
    }
}
